package finger.web;

import finger.domain.Book;
import finger.domain.Page;
import finger.service.BookService;
import finger.service.impl.BookServiceImpl;
import finger.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class BookServlet extends BaseServlet {
    BookService service = new BookServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    protected void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
        pageNo = pageNo + 1;
        Book book = WebUtils.copyParameterToBean(new Book(), req.getParameterMap());
        service.addBook(book);
//        req.getRequestDispatcher("/manager/bookServlet?action=list").forward(req, resp);
        resp.sendRedirect(req.getContextPath() + "/manager/bookServlet?action=page&pageNo=" + pageNo);
    }

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        service.deleteBookById(id);
        resp.sendRedirect(req.getContextPath() + "/manager/bookServlet?action=page&pageNo="+req.getParameter("pageNo"));
    }

    protected void update(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        Book book = WebUtils.copyParameterToBean(new Book(), req.getParameterMap());
        service.updateBook(book);
        resp.sendRedirect(req.getContextPath() + "/manager/bookServlet?action=page&pageNo="+req.getParameter("pageNo"));
    }

    protected void getBook(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        String id = req.getParameter("id");
        int i = WebUtils.parseInt(id, 0);
        Book book = service.queryBookById(i);
        req.setAttribute("book", book);
        req.getRequestDispatcher("/pages/manager/book_edit.jsp?&pageNo="+req.getParameter("pageNo")).forward(req, resp);
    }


    protected void list(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        List<Book> books = service.queryBooks();
        req.setAttribute("book", books);
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req, resp);
    }

    protected void page(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        Page<Book> page = service.page(pageNo, Page.PAGE_SIZE);
        page.setUrl("manager/bookServlet?action=page");
        req.setAttribute("page", page);
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req, resp);
//        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
//        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
//        //2 调用BookService.page(pageNo，pageSize)：Page对象
//        Page<Book> page = service.page(pageNo,pageSize);
////        page.setUrl("manager/bookServlet?action=page");
//        //3 保存Page对象到Request域中
//        req.setAttribute("page",page);
//        //4 请求转发到pages/manager/book_manager.jsp页面
//        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp);
    }
}
